﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DVPCameraType;
using System.Diagnostics;
using Cognex.VisionPro;
using System.Threading;
using System.Runtime.InteropServices;

namespace Delta
{
    public partial class Trigger : Form
    {
        public uint m_handle = 0;
        public bool m_bAeOp = false;
        public int m_n_dev_count = 0;
        string m_strFriendlyName = "";

        public static IntPtr m_ptr_wnd = new IntPtr();
        public static IntPtr m_ptr = new IntPtr();
        public static bool m_b_start = false;

        public static int m_TriggerDelay = 0;
        public static double m_TriggerFilter = 0.0f;
        public static double m_TriggerLoop = 0.0f;
        dvpIntDescr m_DelayDescr = new dvpIntDescr();

        public static bool m_bTriggerMode = false;

        // Display param
        public static Stopwatch m_Stopwatch = new Stopwatch();
        public static Double m_dfDisplayCount = 0;

        public static dvpCameraInfo[] m_info = new dvpCameraInfo[32];
        public static int m_CamCount = 0;
        public Form1 form1;
        public Trigger(Form1 form)
        {
            Control.CheckForIllegalCrossThreadCalls = false;

            InitializeComponent();
            form1 = form;
            // Initialize open mode
            // false: user dvpOpenByName open the camear
            // true : user dvpOpenByUserId open the camear
            UserDefinedName.Checked = false;

            m_ptr_wnd = pictureBox.Handle;
            InitDevList();
            System.Timers.Timer t = new System.Timers.Timer(500);

            // Execute event when the time has arrived.
            t.Elapsed += new System.Timers.ElapsedEventHandler(theout);

            // Set the method of executing.
            t.AutoReset = true;

            // Enable the System.Timers.Timer.Elapsed event.
            t.Enabled = true;

        }

        public bool IsValidHandle(uint handle)
        {
            bool bValidHandle = false;
            dvpStatus status = DVPCamera.dvpIsValid(handle, ref bValidHandle);
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                return bValidHandle;
            }

            return false;
        }

        public void InitDevList()
        {
            dvpStatus status;
            uint i, n = 0;
            dvpCameraInfo dev_info = new dvpCameraInfo();

            // "n" represents the number of cameras that is enumerated successfully, the drop-down list contains each camera's FriendlyName.
            DevNameCombo.Items.Clear();

            // Get the number of cameras that has been connected to a computer.
            status = DVPCamera.dvpRefresh(ref n);
            Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
            m_n_dev_count = (int)n;
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                m_CamCount = 0;

                for (i = 0; i < n; i++)
                {
                    // Acquire each camera's information one by one.
                    status = DVPCamera.dvpEnum(i, ref dev_info);
                    Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                    if (status == dvpStatus.DVP_STATUS_OK)
                    {
                        m_info[m_CamCount] = dev_info;

                        int item = -1;
                        if (!UserDefinedName.Checked)
                        {
                            // add FriendlyName
                            item = DevNameCombo.Items.Add(dev_info.FriendlyName);
                        }
                        else
                        {
                            // add User Define Name
                            item = DevNameCombo.Items.Add(dev_info.UserID);
                        }
                        if (item == 0)
                        {
                            DevNameCombo.SelectedIndex = item;
                        }
                        m_CamCount++;

                        if (item == 0)
                        {
                            DevNameCombo.SelectedIndex = item;
                        }
                    }
                }
            }

            if (n == 0)
            {
                OpenDev.Enabled = false;
                ExposureTimeEdit.Enabled = false;
            }
            else
            {
                OpenDev.Enabled = true;
                ExposureTimeEdit.Enabled = true;
            }

            UpdateControls();            
        }

        public void UpdateControls()
        {
            dvpStatus status;

            if (IsValidHandle(m_handle))
            {
                // The device has been opened at this time.
                // Update and enable the basic controls.
                dvpStreamState state = new dvpStreamState();
                status = DVPCamera.dvpGetStreamState(m_handle, ref state);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                OpenDev.Text = "Close";
                StartPlay.Text = state == dvpStreamState.STATE_STARTED ? ("Stop") : ("Start");
                StartPlay.Enabled = true;
                PropertSet.Enabled = true;

                // Enable the related controls.
                SoftTriggerFire.Enabled = true;


                // Update the window that is related to trigger function.
                bool bTrig = false;

                // Update the enable status of the trigger mode. 
                status = DVPCamera.dvpGetBoolValue(m_handle, dvpParam.V_TRIGGER_MODE_B, ref bTrig);

                // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                TriggerMode.Checked = bTrig;
                if (status != dvpStatus.DVP_STATUS_OK)
                {
                    TriggerMode.Enabled = false;
                    TriggerDelay.Enabled = false;
                    ApplyDelay.Enabled = false;
                    SoftTriggerFire.Enabled = false;
                }
                else
                {
                    TriggerMode.Enabled = state != dvpStreamState.STATE_STARTED;
                    TriggerMode.Checked = bTrig;

                    Delay.Enabled = bTrig;
                    TriggerDelay.Enabled = bTrig;
                    ApplyDelay.Enabled = bTrig;
                    SoftTriggerFire.Enabled = bTrig;
                }

                // Update the external trigger input status of the related IO.
                    
         
                // The following descriptions of the information will be used to update the range of values in the edit box.
                status = DVPCamera.dvpGetIntValue(m_handle, dvpParam.V_TRIGGER_DELAY_I, ref m_TriggerDelay, ref m_DelayDescr);
                if (status != dvpStatus.DVP_STATUS_OK)
                {
                    Delay.Enabled = false;
                    TriggerDelay.Enabled = false;
                    ApplyDelay.Enabled = false;
                }
                else 
                {
                    // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                    TriggerDelay.Maximum = (decimal)m_DelayDescr.iMax;
                    TriggerDelay.Minimum = (decimal)m_DelayDescr.iMin;


                    // Update values in the edit box. 
                    if (m_TriggerDelay > m_DelayDescr.iMax)
                        m_TriggerDelay = m_DelayDescr.iMax;
                    if (m_TriggerDelay < m_DelayDescr.iMin)
                        m_TriggerDelay = m_DelayDescr.iMin;
                    TriggerDelay.Value = (decimal)m_TriggerDelay;

                }             

                bool bLineInverter = false;
                //line inverter
                status = DVPCamera.dvpGetBoolValue(m_handle, dvpParam.V_LINE_INVERTER_B, ref bLineInverter);
                LineInverter.Checked = bLineInverter;
                LineInverter.Enabled = true;


                LineSelectorComb.Items.Clear();

                int nCurEnumValue = 0;
                uint nSupportNum = 0;
                int[] nSupportValue = new int[64];

                //line selector
                status = DVPCamera.dvpGetEnumValue(m_handle, dvpParam.V_LINE_SELECTOR_E, ref nCurEnumValue, nSupportValue, ref nSupportNum);

                if (status != dvpStatus.DVP_STATUS_OK)
                {
                    LineSelectorComb.Enabled = false;
                    LineSelector.Enabled = false;
                }
                else 
                {
                    uint nCurIndex = 0;
                    for (uint i = 0; i < nSupportNum; i++)
                    {
                        if (nSupportValue[i] == nCurEnumValue)
                        {
                            nCurIndex = i;
                        }

                        dvpEnumDescr pEnumDescr = new dvpEnumDescr();
                        //line selector descrtion
                        DVPCamera.dvpGetEnumDescr(m_handle, dvpParam.V_LINE_SELECTOR_E, i, ref pEnumDescr);

                        LineSelectorComb.Items.Add(pEnumDescr.szEnumName);
                    }

                    LineSelectorComb.SelectedIndex = (int)nCurIndex;
                    LineSelectorComb.Enabled = true;
                    LineSelector.Enabled = true;
                }


                LineModeComb.Items.Clear();
                nCurEnumValue = 0;
                nSupportNum = 0;
                Array.Clear(nSupportValue, 0, nSupportValue.Length);

                //line mode
                status = DVPCamera.dvpGetEnumValue(m_handle, dvpParam.V_LINE_MODE_E, ref nCurEnumValue, nSupportValue, ref nSupportNum);

                if (status != dvpStatus.DVP_STATUS_OK)
                {
                    LineModeComb.Enabled = false;
                    LineMode.Enabled = false;
                }
                else
                {
                    uint nCurIndex = 0;
                    for (uint i = 0; i < nSupportNum; i++)
                    {
                        if (nSupportValue[i] == nCurEnumValue)
                        {
                            nCurIndex = i;
                        }

                        dvpEnumDescr pEnumDescr = new dvpEnumDescr();
                        //line mode descrtion
                        DVPCamera.dvpGetEnumDescr(m_handle, dvpParam.V_LINE_MODE_E, i, ref pEnumDescr);

                        LineModeComb.Items.Add(pEnumDescr.szEnumName);
                    }

                    LineModeComb.SelectedIndex = (int)nCurIndex;
                    LineModeComb.Enabled = true;
                    LineMode.Enabled = true;
                }


                LineSourceComb.Items.Clear();
                nCurEnumValue = 0;
                nSupportNum = 0;
                Array.Clear(nSupportValue, 0, nSupportValue.Length);

                //line source
                status = DVPCamera.dvpGetEnumValue(m_handle, dvpParam.V_LINE_SOURCE_E, ref nCurEnumValue, nSupportValue, ref nSupportNum);

                if (status != dvpStatus.DVP_STATUS_OK)
                {
                    LineSourceComb.Enabled = false;
                    LineSource.Enabled = false;
                }
                else
                {
                    uint nCurIndex = 0;
                    for (uint i = 0; i < nSupportNum; i++)
                    {
                        if (nSupportValue[i] == nCurEnumValue)
                        {
                            nCurIndex = i;
                        }

                        dvpEnumDescr pEnumDescr = new dvpEnumDescr();
                        //line source descrtion
                        DVPCamera.dvpGetEnumDescr(m_handle, dvpParam.V_LINE_SOURCE_E, i, ref pEnumDescr);

                        LineSourceComb.Items.Add(pEnumDescr.szEnumName);
                    }

                    LineSourceComb.SelectedIndex = (int)nCurIndex;
                    LineSourceComb.Enabled = true;
                    LineSource.Enabled = true;
                }
            }
            else
            {
                // No device is opened at this time.
                // Update the basic controls.
                OpenDev.Text = "Open";
                StartPlay.Enabled = false;
                PropertSet.Enabled = false;

                if (DevNameCombo.Items.Count == 0)
                {
                    // No device exists.
                    OpenDev.Enabled = false;
                }
                else
                {
                    OpenDev.Enabled = true;
                }

                // Update the related controls.
                TriggerDelay.Enabled = false;
                TriggerMode.Enabled = false;
                SoftTriggerFire.Enabled = false;
                ApplyDelay.Enabled = false;
                Delay.Enabled = false;

                LineInverter.Enabled = false;
                LineSelectorComb.Enabled = false;
                LineModeComb.Enabled = false;
                LineSourceComb.Enabled = false;

                LineSelector.Enabled = false;
                LineMode.Enabled = false;
                LineSource.Enabled = false;
            }
        }

        public void UpdateControlExposure()
        {
            float fExpoTime = 0.0f;
            dvpFloatDescr ExpoTimeDescr = new dvpFloatDescr();
            dvpStatus status;

            // Get the descriptive information about the exposure time.
            status = DVPCamera.dvpGetFloatValue(m_handle, dvpParam.V_EXPOSURE_TIME_F, ref fExpoTime, ref ExpoTimeDescr);
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                // Set the range of the exposure time.
                ExposureTimeEdit.Maximum = (decimal)ExpoTimeDescr.fMax;
                ExposureTimeEdit.Minimum = (decimal)ExpoTimeDescr.fMin;

                // Set the initial value of the exposure time. 
                ExposureTimeEdit.Value = (decimal)fExpoTime;

            }
            else
            {
                //Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
            }
        }



        public void theout(object source, System.Timers.ElapsedEventArgs e)
        {
            if (IsValidHandle(m_handle))
            {
                // Update the information of frame rate.
                dvpFrameCount count = new dvpFrameCount();
                dvpStatus status = DVPCamera.dvpGetFrameCount(m_handle, ref count);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                string str;
                if (m_dfDisplayCount == 0 || m_bTriggerMode)
                {
                    str = m_strFriendlyName + " [" + count.uFrameCount.ToString() + " frames, "
                        + string.Format("{0:#0.00}", count.fFrameRate) + " fps]";
                }
                else
                {
                    str = m_strFriendlyName + " [" + count.uFrameCount.ToString() + " frames, "
                        + string.Format("{0:#0.00}", count.fFrameRate) + " fps, Display "
                        + string.Format("{0:#0.00}", m_dfDisplayCount * 1000.0f / m_Stopwatch.ElapsedMilliseconds) + " fps]";
                }

                this.Text = str;
            }
        }

        private void ScanDev_Click(object sender, EventArgs e)
        {
            InitDevList();
        }

        private DVPCamera.dvpStreamCallback _proc;

        // Callback function that used for receiving data.
        public  int _dvpStreamCallback(/*dvpHandle*/uint handle, dvpStreamEvent _event, /*void **/IntPtr pContext, ref dvpFrame refFrame, /*void **/IntPtr pBuffer)
        {

            Bitmap bitmap;
            if (refFrame.format == dvpImageFormat.FORMAT_MONO)
            {
                bitmap = new Bitmap(refFrame.iWidth, refFrame.iHeight, PixelFormat.Format8bppIndexed);

                ColorPalette ncp = bitmap.Palette;
                for (int i = 0; i < 256; i++)
                    ncp.Entries[i] = Color.FromArgb(255, i, i, i);
                bitmap.Palette = ncp;
            }
            else
            {
                bitmap = new Bitmap(refFrame.iWidth, refFrame.iHeight, PixelFormat.Format24bppRgb);
            }

            CogImage8Root cogImage8 = new CogImage8Root();
            cogImage8.Initialize(bitmap.Width, bitmap.Height, pBuffer, bitmap.Width, null);//bitmap转换成CogImage8Grey
            CogImage8Grey cogImage = new CogImage8Grey();
            cogImage.SetRoot(cogImage8);
            form1.formVpToolBlock. ImageToolBlockRun(cogImage, 0);
            //form1.cogRecordDisplay1.Image =cogImage;

            bool bDisplay = false;

            if (m_dfDisplayCount == 0)
            {
                m_Stopwatch.Restart();
                bDisplay = false;
            }
            else
            {
                if (m_Stopwatch.ElapsedMilliseconds - (long)(m_dfDisplayCount * 33.3f) >= 33)
                {
                    bDisplay = false;
                }
            }

            if (bDisplay || m_bTriggerMode)
            {
                m_dfDisplayCount++;

                // It demonstrates the usual video drawing,and it is not recommended to take a longer time operation in the callback function,
                // in order to avoid affecting the frame rate and the real-time of acquiring images.
                // The acquired image data buffer is valid only before the function returns,so the buffer pointer should not be passed out, 
                // however, the user can malloc memory and copy image data.

                dvpStatus status = DVPCamera.dvpDrawPicture(ref refFrame, pBuffer,
                    m_ptr_wnd, (IntPtr)0, (IntPtr)0);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
            }
            return 1;
        }

        public void OpenDev_Click(object sender, EventArgs e)
        {
            dvpStatus status = dvpStatus.DVP_STATUS_OK;


            if (!IsValidHandle(m_handle))
            {
                if (DevNameCombo.Text != "")
                {
                    if (UserDefinedName.Checked)
                    {
                        // Open the specific device by the selected user define name.
                        status = DVPCamera.dvpOpenByUserId(DevNameCombo.Text, dvpOpenMode.OPEN_NORMAL, ref m_handle);
                    }
                    else
                    {
                        // Open the specific device by the selected FriendlyName.
                        status = DVPCamera.dvpOpenByName(DevNameCombo.Text, dvpOpenMode.OPEN_NORMAL, ref m_handle);
                    }

                    if (status != dvpStatus.DVP_STATUS_OK)
                    {
                        MessageBox.Show("Open the device failed!");
                    }
                    else
                    {
                        m_strFriendlyName = DevNameCombo.Text;

                        // If it needs to display images ,the user should register a callback function and finish the operation of drawing pictures in the registered callback function.
                        // Note: Drawing pictures in the callback function maybe generate some delays for acquiring image data by the use of "dvpGetFrame".
                        _proc = _dvpStreamCallback;
                        using (Process curProcess = Process.GetCurrentProcess())
                        using (ProcessModule curModule = curProcess.MainModule)
                        {
                            status = DVPCamera.dvpRegisterStreamCallback(m_handle, _proc, dvpStreamEvent.STREAM_EVENT_FRAME_THREAD, m_ptr);
                            Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                        }
                    }
                }
            }
            else
            {
                // check camear
                dvpStreamState StreamState = new dvpStreamState();
                status = DVPCamera.dvpGetStreamState(m_handle, ref StreamState);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                if (StreamState == dvpStreamState.STATE_STARTED)
                {
                    // init display count is 0
                    m_dfDisplayCount = 0;

                    // stop camera
                    status = DVPCamera.dvpStop(m_handle);
                    Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                }

                // cloas camera
                status = DVPCamera.dvpClose(m_handle);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                m_handle = 0;
                pictureBox.Refresh();
            }
            UpdateControlExposure();
            UpdateControls();
            
        }

        public void StartPlay_Click(object sender, EventArgs e)
        {
            // init display count is 0
            m_dfDisplayCount = 0;

            if (IsValidHandle(m_handle))
            {
                dvpStreamState state = new dvpStreamState();
                dvpStatus status;

                // Implement a button to start and stop according to the current video's status.
                status = DVPCamera.dvpGetStreamState(m_handle, ref state);

                if (state == dvpStreamState.STATE_STARTED)
                {
                    status = DVPCamera.dvpStop(m_handle);
                }
                else
                {
                    if (!TriggerMode.Enabled)
                    {
                        m_bTriggerMode = false;
                    }
                    else
                    {
                       
                        m_bTriggerMode = TriggerMode.Checked;
                        m_bTriggerMode = false;
                    }

                    status = DVPCamera.dvpStart(m_handle);
                }
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
            }

            UpdateControls();
        }

        private void PropertSet_Click(object sender, EventArgs e)
        {
            if (IsValidHandle(m_handle))
            {
                dvpStatus status = DVPCamera.dvpShowPropertyModalDialog(m_handle, this.Handle);

                // At this time some configurations may change, synchronize it to the window GUI.
                UpdateControls();
            }
        }

        private void TriggerMode_CheckedChanged(object sender, EventArgs e)
        {
            if (IsValidHandle(m_handle))
            {
                dvpStatus status = new dvpStatus();

                // get Stream State
                dvpStreamState StreamState = new dvpStreamState();
                status = DVPCamera.dvpGetStreamState(m_handle, ref StreamState);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                if (StreamState == dvpStreamState.STATE_STARTED)
                {
                    // Stop the video stream.
                    status = DVPCamera.dvpStop(m_handle);
                    Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                }

                // Enable/disable the trigger mode.
                status = DVPCamera.dvpSetBoolValue(m_handle, dvpParam.V_TRIGGER_MODE_B, TriggerMode.Checked);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                if (StreamState == dvpStreamState.STATE_STARTED)
                {
                    // Start the video stream.
                    status = DVPCamera.dvpStart(m_handle);
                    Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                }
                UpdateControls();
            }
        }

        public void SoftTriggerFire_Click(object sender, EventArgs e)
        {
            if (IsValidHandle(m_handle))
            {
                // Once execution of this function is equivalent to the generation of an external trigger.
                // Note:If the exposure time is very long, clicking on the "Soft Trigger the Fire" too quick may cause the failure of the trigger,
                // because the previous frame probably in a state of exposing continuously or output incompletely.
                dvpStatus status = DVPCamera.dvpSetCommandValue(m_handle, "TriggerSoftware");
            }
        }

        private void ApplyDelay_Click(object sender, EventArgs e)
        {
            if (IsValidHandle(m_handle))
            {
                m_TriggerDelay = (int)TriggerDelay.Value;

                dvpStatus status = DVPCamera.dvpSetIntValue(m_handle, dvpParam.V_TRIGGER_DELAY_I, m_TriggerDelay);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                status = DVPCamera.dvpGetIntValue(m_handle, dvpParam.V_TRIGGER_DELAY_I, ref m_TriggerDelay, ref m_DelayDescr);
                if (status == dvpStatus.DVP_STATUS_OK)
                {
                    TriggerDelay.Value = (int)m_TriggerDelay;
                }
            }
        }

        private void Trigger_FormClosing(object sender, FormClosingEventArgs e)
        {
            //dvpStatus status;
            //dvpStreamState state = new dvpStreamState();
            //if (IsValidHandle(m_handle))
            //{
            //    status = DVPCamera.dvpGetStreamState(m_handle, ref state);
            //    // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

            //    if (state == dvpStreamState.STATE_STARTED)
            //    {
            //        status = DVPCamera.dvpStop(m_handle);
            //        // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

            //    }

            //    status = DVPCamera.dvpClose(m_handle);
            //    // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

            //    m_handle = 0;
            //}
            this.Hide();
        }  


        private void ResizeWindows()
        {
            if (IsValidHandle(m_handle))
            {
                dvpRegion roi;
                roi = new dvpRegion();
                dvpStatus status;
                status = DVPCamera.dvpGetRoi(m_handle, ref roi);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                pictureBox.Width = this.Width - pictureBox.Left;
                pictureBox.Height = this.Height - pictureBox.Top;

                if (pictureBox.Width * roi.H > pictureBox.Height * roi.W)
                {
                    pictureBox.Width = pictureBox.Height * roi.W / roi.H;
                }
                else
                {
                    pictureBox.Height = pictureBox.Width * roi.H / roi.W;
                }
            }
        }

        private void Trigger_Resize(object sender, EventArgs e)
        {
            ResizeWindows();
        }

        private void UserDefineName_CheckedChanged(object sender, EventArgs e)
        {
            // save cur sel item
            string strName;
            strName = DevNameCombo.Text;

            // reset m_listDevices values
            DevNameCombo.Items.Clear();

            for (int i = 0; i < m_CamCount; i++)
            {
                int item = -1;
                if (!UserDefinedName.Checked)
                {
                    item = DevNameCombo.Items.Add(m_info[i].FriendlyName);
                    if (strName == m_info[i].UserID)
                    {
                        DevNameCombo.SelectedIndex = item;
                    }
                }
                else
                {
                    // check User define name is null
                    if (m_info[i].UserID.Length == 0)
                        continue;

                    item = DevNameCombo.Items.Add(m_info[i].UserID);
                    if (strName == m_info[i].FriendlyName)
                    {
                        DevNameCombo.SelectedIndex = item;
                    }
                }
            }
        }

        private void LineInverter_CheckedChanged(object sender, EventArgs e)
        {
            // Enable/disable the line inverter.
            dvpStatus status = DVPCamera.dvpSetBoolValue(m_handle, dvpParam.V_LINE_INVERTER_B, LineInverter.Checked);
        }

        private void LineSelectorComb_SelectedIndexChanged(object sender, EventArgs e)
        {
            int nCurSel = LineSelectorComb.SelectedIndex;

            dvpEnumDescr pEnumDescr = new dvpEnumDescr();
            //get the line selector description information by index
            dvpStatus status = DVPCamera.dvpGetEnumDescr(m_handle, dvpParam.V_LINE_SELECTOR_E, (uint)nCurSel, ref pEnumDescr);
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                //set the line selector 's value
                status = DVPCamera.dvpSetEnumValue(m_handle, dvpParam.V_LINE_SELECTOR_E, pEnumDescr.iEnumValue);
            }
        }

        private void LineModeComb_SelectedIndexChanged(object sender, EventArgs e)
        {
            int nCurSel = LineModeComb.SelectedIndex;

            dvpEnumDescr pEnumDescr = new dvpEnumDescr();
            //get the line mode description information by index
            dvpStatus status = DVPCamera.dvpGetEnumDescr(m_handle, dvpParam.V_LINE_MODE_E, (uint)nCurSel, ref pEnumDescr);
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                //set the line mode 's value
                status = DVPCamera.dvpSetEnumValue(m_handle, dvpParam.V_LINE_MODE_E, pEnumDescr.iEnumValue);
            }
        }

        private void LineSourceComb_SelectedIndexChanged(object sender, EventArgs e)
        {
            int nCurSel = LineSourceComb.SelectedIndex;

            dvpEnumDescr pEnumDescr = new dvpEnumDescr();
            //get the line source description information by index
            dvpStatus status = DVPCamera.dvpGetEnumDescr(m_handle, dvpParam.V_LINE_SOURCE_E, (uint)nCurSel, ref pEnumDescr);
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                //set the line source 's value
                status = DVPCamera.dvpSetEnumValue(m_handle, dvpParam.V_LINE_SOURCE_E, pEnumDescr.iEnumValue);
            }
        }

        private void ExposureTimeApply_Click(object sender, EventArgs e)
        {
            dvpStatus status;
            float f_time = 0.0f;
            if (IsValidHandle(m_handle))
            {
                if (ExposureTimeEdit.Maximum < ExposureTimeEdit.Value + 1)
                {
                    ExposureTimeEdit.Value = ExposureTimeEdit.Maximum - 1;
                }
                if (ExposureTimeEdit.Minimum > ExposureTimeEdit.Value - 1)
                {
                    ExposureTimeEdit.Value = ExposureTimeEdit.Minimum + 1;
                }

                status = DVPCamera.dvpSetFloatValue(m_handle, dvpParam.V_EXPOSURE_TIME_F, (float)ExposureTimeEdit.Value);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                //  Get the value of the exposure time again,there are differences between the set value and the obtained value for the reason of accuracy(step value),
                //  it is subject to the obtained value.
                dvpFloatDescr ExpoTimeDescr = new dvpFloatDescr();
                status = DVPCamera.dvpGetFloatValue(m_handle, dvpParam.V_EXPOSURE_TIME_F, ref f_time, ref ExpoTimeDescr);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                // Set the text of the exposure time.
                ExposureTimeEdit.Value = (long)f_time;
            }
        }
    }
}
